package com.aurora.spider.houyuantuan;

import com.aurora.event.PersonShipEvent;
import com.geccocrawler.gecco.annotation.PipelineName;
import com.geccocrawler.gecco.pipeline.Pipeline;
import com.geccocrawler.gecco.request.HttpRequest;
import com.geccocrawler.gecco.scheduler.StartSchedulerContext;
import com.geccocrawler.gecco.spider.HrefBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by jackie on 18/1/15.
 */
@PipelineName("guanxiDealLine")
@Component
@Slf4j
public class GuanxiDealLine implements Pipeline<Guanxi>, ApplicationContextAware {

    private static ApplicationContext applicationContext;
    @Override
    public void process(Guanxi guanxi) {
        Long starId = guanxi.getStarId();
        String userName = guanxi.getUserName();

        List<HitProject> projectList = guanxi.getProjectList();
        //处理关系
        PersonShip personShip = new PersonShip();
        personShip.setStarId(starId);
        personShip.setRelaList(projectList);
        applicationContext.publishEvent(new PersonShipEvent(personShip));
        for (HitProject hitProject : projectList) {
            //自己的就不跳了
            if (hitProject.getPersonName() == null || userName.equals(hitProject.getPersonName())) {
                continue;
            }
            List<HrefBean> hrefs = hitProject.getUrls();
            for (HrefBean href : hrefs) {
                String url = href.getUrl();
                HttpRequest currRequest = guanxi.getRequest();
                StartSchedulerContext.into(currRequest.subRequest(url));
            }
        }
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
